<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 创建项目，导入jar包  Spring的jar包 Mybatis的jar包 Mybatis-Spring整合包，即mybatis-spring-1.3.2.jar MySql的数据库驱动jar包 数据库连接池的jar包 JSTL所需的jar包">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="JakartaEE-Spring、MyBatis和SpringMVC的整合(一)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(一)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 创建项目，导入jar包  Spring的jar包 Mybatis的jar包 Mybatis-Spring整合包，即mybatis-spring-1.3.2.jar MySql的数据库驱动jar包 数据库连接池的jar包 JSTL所需的jar包">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d5cab516a74d3334.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5e70a31376478f0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-91a6dd9d6581594d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-4d95783e53871ca4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-1c034fe7817df400.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-4c29b227d9495638.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:50:59.721Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JakartaEE-Spring、MyBatis和SpringMVC的整合(一)">
<meta name="twitter:description" content="####1. 创建项目，导入jar包  Spring的jar包 Mybatis的jar包 Mybatis-Spring整合包，即mybatis-spring-1.3.2.jar MySql的数据库驱动jar包 数据库连接池的jar包 JSTL所需的jar包">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-d5cab516a74d3334.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(一)/"/>





  <title>JakartaEE-Spring、MyBatis和SpringMVC的整合(一) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(一)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JakartaEE-Spring、MyBatis和SpringMVC的整合(一)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 创建项目，导入jar包</p>
<ul>
<li><a href="http://repo.springsource.org/libs-release-local/org/springframework/spring/" target="_blank" rel="noopener">Spring的jar包</a></li>
<li><a href="https://github.com/mybatis/mybatis-3" target="_blank" rel="noopener">Mybatis的jar包</a></li>
<li><a href="http://blog.mybatis.org/" target="_blank" rel="noopener">Mybatis-Spring整合包</a>，即mybatis-spring-1.3.2.jar</li>
<li><a href="https://www.mysql.com/products/connector/" target="_blank" rel="noopener">MySql的数据库驱动jar包</a></li>
<li><a href="http://commons.apache.org/" target="_blank" rel="noopener">数据库连接池的jar包</a></li>
<li><a href="http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/" target="_blank" rel="noopener">JSTL所需的jar包</a><br><img src="https://upload-images.jianshu.io/upload_images/3110861-d5cab516a74d3334.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><a id="more"></a>
</li>
</ul>
<p>####2. 整合思路<br><img src="https://upload-images.jianshu.io/upload_images/3110861-5e70a31376478f0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"><br>Spring在进行管理时，是很有条理的，每个层都由Spring管理，然后不同的层可以调用其它层，Handler调用service，service调用mapper等。</p>
<ul>
<li>Dao层<br>1&gt;. SqlMapConfig.xml，空文件即可。<br>2&gt;.applicationContext-dao.xml<br>– a) 数据库连接池<br>– b) SqlSessionFactory对象，需要Spring和MyBatis的整合包。<br>– c) 配置mapper文件扫描器。</li>
<li>Service层<br>1&gt;. applicationContext-service.xml文件中配置包扫描器，扫描带@service注解的类。<br>2&gt;. applicationContext-trans.xml文件中配置事务。</li>
<li>表现层<br>1&gt;. 包扫描器，扫描带@Controller注解的类。<br>2&gt;. 配置注解驱动。<br>3&gt;. 配置视图解析器</li>
<li>web.xml<br>1&gt;. 在web.xml文件中配置前端控制器。<br>####3. 项目使用到的springmvc.sql<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">SET FOREIGN_KEY_CHECKS=0;</span><br><span class="line"></span><br><span class="line">-- ----------------------------</span><br><span class="line">-- Table structure for items</span><br><span class="line">-- ----------------------------</span><br><span class="line">DROP TABLE IF EXISTS `items`;</span><br><span class="line">CREATE TABLE `items` (</span><br><span class="line">  `id` int(11) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  `name` varchar(32) NOT NULL COMMENT &apos;商品名称&apos;,</span><br><span class="line">  `price` float(10,1) NOT NULL COMMENT &apos;商品定价&apos;,</span><br><span class="line">  `detail` text COMMENT &apos;商品描述&apos;,</span><br><span class="line">  `pic` varchar(64) DEFAULT NULL COMMENT &apos;商品图片&apos;,</span><br><span class="line">  `createtime` datetime NOT NULL COMMENT &apos;生产日期&apos;,</span><br><span class="line">  PRIMARY KEY (`id`)</span><br><span class="line">) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;</span><br><span class="line"></span><br><span class="line">-- ----------------------------</span><br><span class="line">-- Records of items</span><br><span class="line">-- ----------------------------</span><br><span class="line">INSERT INTO `items` VALUES (&apos;1&apos;, &apos;台式机&apos;, &apos;3000.0&apos;, &apos;该电脑质量非常好！！！！&apos;, null, &apos;2016-02-03 13:22:53&apos;);</span><br><span class="line">INSERT INTO `items` VALUES (&apos;2&apos;, &apos;笔记本&apos;, &apos;6000.0&apos;, &apos;笔记本性能好，质量好！！！！！&apos;, null, &apos;2015-02-09 13:22:57&apos;);</span><br><span class="line">INSERT INTO `items` VALUES (&apos;3&apos;, &apos;背包&apos;, &apos;200.0&apos;, &apos;名牌背包，容量大质量好！！！！&apos;, null, &apos;2015-02-06 13:23:02&apos;);</span><br><span class="line"></span><br><span class="line">-- ----------------------------</span><br><span class="line">-- Table structure for user</span><br><span class="line">-- ----------------------------</span><br><span class="line">DROP TABLE IF EXISTS `user`;</span><br><span class="line">CREATE TABLE `user` (</span><br><span class="line">  `id` int(11) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  `username` varchar(32) NOT NULL COMMENT &apos;用户名称&apos;,</span><br><span class="line">  `birthday` date DEFAULT NULL COMMENT &apos;生日&apos;,</span><br><span class="line">  `sex` char(1) DEFAULT NULL COMMENT &apos;性别&apos;,</span><br><span class="line">  `address` varchar(256) DEFAULT NULL COMMENT &apos;地址&apos;,</span><br><span class="line">  PRIMARY KEY (`id`)</span><br><span class="line">) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;</span><br><span class="line"></span><br><span class="line">-- ----------------------------</span><br><span class="line">-- Records of user</span><br><span class="line">-- ----------------------------</span><br><span class="line">INSERT INTO `user` VALUES (&apos;1&apos;, &apos;王五&apos;, null, &apos;2&apos;, null);</span><br><span class="line">INSERT INTO `user` VALUES (&apos;10&apos;, &apos;张三&apos;, &apos;2014-07-10&apos;, &apos;1&apos;, &apos;北京市&apos;);</span><br><span class="line">INSERT INTO `user` VALUES (&apos;16&apos;, &apos;张小明&apos;, null, &apos;1&apos;, &apos;河南郑州&apos;);</span><br><span class="line">INSERT INTO `user` VALUES (&apos;22&apos;, &apos;陈小明&apos;, null, &apos;1&apos;, &apos;河南郑州&apos;);</span><br><span class="line">INSERT INTO `user` VALUES (&apos;24&apos;, &apos;张三丰&apos;, null, &apos;1&apos;, &apos;河南郑州&apos;);</span><br><span class="line">INSERT INTO `user` VALUES (&apos;25&apos;, &apos;陈小明&apos;, null, &apos;1&apos;, &apos;河南郑州&apos;);</span><br><span class="line">INSERT INTO `user` VALUES (&apos;26&apos;, &apos;王五&apos;, null, null, null);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>####4. Dao层</p>
<p>######1). 在Java Resources下创建源码包config<br><img src="https://upload-images.jianshu.io/upload_images/3110861-91a6dd9d6581594d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>######2). 在Config源码包下创建mybatis/SqlMapConfig.xml<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE configuration PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;</span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">	&lt;!-- 添加别名 --&gt;</span><br><span class="line">	&lt;typeAliases&gt;</span><br><span class="line">		&lt;!-- 所有类均已类名方法，并且不区分大小写 --&gt;</span><br><span class="line">		&lt;package name=&quot;com.mazaiting.pojo&quot;/&gt;</span><br><span class="line">	&lt;/typeAliases&gt;</span><br><span class="line">&lt;/configuration&gt;</span><br></pre></td></tr></table></figure></p>
<p>######3). 在Config源码包下创建Spring配置文件–applicationContext-dao.xml<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">	xmlns:context=&quot;http://www.springframework.org/schema/context&quot; </span><br><span class="line">	xmlns:p=&quot;http://www.springframework.org/schema/p&quot;</span><br><span class="line">	xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot; </span><br><span class="line">	xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;</span><br><span class="line">	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">	xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans </span><br><span class="line">						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd</span><br><span class="line">    					http://www.springframework.org/schema/context </span><br><span class="line">    					http://www.springframework.org/schema/context/spring-context-4.0.xsd</span><br><span class="line">    					http://www.springframework.org/schema/aop </span><br><span class="line">    					http://www.springframework.org/schema/aop/spring-aop-4.0.xsd </span><br><span class="line">    					http://www.springframework.org/schema/tx </span><br><span class="line">    					http://www.springframework.org/schema/tx/spring-tx-4.0.xsd</span><br><span class="line">    					http://www.springframework.org/schema/util </span><br><span class="line">    					http://www.springframework.org/schema/util/spring-util-4.0.xsd&quot;&gt;</span><br><span class="line"></span><br><span class="line">	&lt;!-- 加载配置文件 --&gt;</span><br><span class="line">	&lt;context:property-placeholder location=&quot;classpath:db.properties&quot;/&gt;</span><br><span class="line">	&lt;!-- 数据库连接池 --&gt;</span><br><span class="line">	&lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.commons.dbcp2.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;</span><br><span class="line">		&lt;!-- 驱动名称 --&gt;</span><br><span class="line">		&lt;property name=&quot;driverClassName&quot; value=&quot;$&#123;jdbc.driver&#125;&quot;/&gt;</span><br><span class="line">		&lt;!-- 数据库地址链接 --&gt;</span><br><span class="line">		&lt;property name=&quot;url&quot; value=&quot;$&#123;jdbc.url&#125;&quot;/&gt;</span><br><span class="line">		&lt;!-- 用户名 --&gt;</span><br><span class="line">		&lt;property name=&quot;username&quot; value=&quot;$&#123;jdbc.username&#125;&quot;/&gt;</span><br><span class="line">		&lt;!-- 密码 --&gt;</span><br><span class="line">		&lt;property name=&quot;password&quot; value=&quot;$&#123;jdbc.password&#125;&quot;/&gt;</span><br><span class="line">		&lt;!-- 最大连接数 --&gt;</span><br><span class="line">		&lt;property name=&quot;maxTotal&quot; value=&quot;10&quot;/&gt;</span><br><span class="line">		&lt;!-- 设置最大空闲数 --&gt;</span><br><span class="line">		&lt;property name=&quot;maxIdle&quot; value=&quot;5&quot;/&gt;</span><br><span class="line">	&lt;/bean&gt;</span><br><span class="line">	&lt;!-- mapper配置 --&gt;</span><br><span class="line">	&lt;!-- 让Spring管理sqlSessionFactory --&gt;</span><br><span class="line">	&lt;bean id=&quot;sqlSessionFactory&quot; class=&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;&gt;</span><br><span class="line">		&lt;!-- 数据库连接池 --&gt;</span><br><span class="line">		&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;</span><br><span class="line">		&lt;!-- 加载mybatis的全局配置文件 --&gt;</span><br><span class="line">		&lt;property name=&quot;configLocation&quot; value=&quot;classpath:mybatis/SqlMapConfig.xml&quot;/&gt;</span><br><span class="line">	&lt;/bean&gt;</span><br><span class="line">	&lt;!-- 配置Mapper扫描器 --&gt;</span><br><span class="line">	&lt;bean class=&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;&gt;</span><br><span class="line">		&lt;!-- 配置扫描包,如果有多个，使用逗号分隔 --&gt;</span><br><span class="line">		&lt;property name=&quot;basePackage&quot; value=&quot;com.mazaiting.mapper&quot;/&gt;</span><br><span class="line">	&lt;/bean&gt;	</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>注意：一定记得加载mybatis的全局配置文件，虽然这个全局配置文件是空的，但是这个全局配置文件是必不可少的</strong><br><strong>db.properties配置文件的内容</strong>：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">jdbc.driver=com.mysql.jdbc.Driver</span><br><span class="line">jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8</span><br><span class="line">jdbc.username=root</span><br><span class="line">jdbc.password=root</span><br></pre></td></tr></table></figure></p>
<p><strong>log4j.properties配置文件内容</strong>：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># Global logging configuration</span><br><span class="line">log4j.rootLogger=DEBUG, stdout</span><br><span class="line"># Console output...</span><br><span class="line">log4j.appender.stdout=org.apache.log4j.ConsoleAppender</span><br><span class="line">log4j.appender.stdout.layout=org.apache.log4j.PatternLayout</span><br><span class="line">log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n</span><br></pre></td></tr></table></figure></p>
<p>####5. Service</p>
<p>######1). 在Config源码包下创建/spring/applicationContext-service.xml文件，并配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">    xmlns:context=&quot;http://www.springframework.org/schema/context&quot; </span><br><span class="line">    xmlns:p=&quot;http://www.springframework.org/schema/p&quot;</span><br><span class="line">    xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot; </span><br><span class="line">    xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;</span><br><span class="line">    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans </span><br><span class="line">    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd</span><br><span class="line">    http://www.springframework.org/schema/context </span><br><span class="line">    http://www.springframework.org/schema/context/spring-context-4.0.xsd</span><br><span class="line">    http://www.springframework.org/schema/aop </span><br><span class="line">    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd </span><br><span class="line">    http://www.springframework.org/schema/tx </span><br><span class="line">    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd</span><br><span class="line">    http://www.springframework.org/schema/util </span><br><span class="line">    http://www.springframework.org/schema/util/spring-util-4.0.xsd&quot;&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;!-- 自定义 --&gt;</span><br><span class="line">    &lt;!-- 配置包扫描器，扫描带@Service注解的类 --&gt;</span><br><span class="line">    &lt;context:component-scan base-package=&quot;com.mazaiting.service&quot;/&gt;</span><br><span class="line">    </span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<p>######2). 在Config源码包下创建/spring/applicationContext-trans.xml，并配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">    xmlns:context=&quot;http://www.springframework.org/schema/context&quot; </span><br><span class="line">    xmlns:p=&quot;http://www.springframework.org/schema/p&quot;</span><br><span class="line">    xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot; </span><br><span class="line">    xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;</span><br><span class="line">    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans </span><br><span class="line">    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd</span><br><span class="line">    http://www.springframework.org/schema/context </span><br><span class="line">    http://www.springframework.org/schema/context/spring-context-4.0.xsd</span><br><span class="line">    http://www.springframework.org/schema/aop </span><br><span class="line">    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd </span><br><span class="line">    http://www.springframework.org/schema/tx </span><br><span class="line">    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd</span><br><span class="line">    http://www.springframework.org/schema/util </span><br><span class="line">    http://www.springframework.org/schema/util/spring-util-4.0.xsd&quot;&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;!-- 事务管理器，使用的Spring JDBC的事务管理器 --&gt;</span><br><span class="line">    &lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;&gt;</span><br><span class="line">    	&lt;!-- 数据源 --&gt;</span><br><span class="line">    	&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">    &lt;!-- 通知 --&gt;</span><br><span class="line">    &lt;tx:advice id=&quot;txAdvice&quot; transaction-manager=&quot;transactionManager&quot;&gt;</span><br><span class="line">    	&lt;tx:attributes&gt;</span><br><span class="line">    		&lt;!-- 传播行为,数据库操作的方法尽可能按照下列的名称命名 --&gt;</span><br><span class="line">    		&lt;tx:method name=&quot;save*&quot; propagation=&quot;REQUIRED&quot;/&gt;</span><br><span class="line">    		&lt;tx:method name=&quot;insert*&quot; propagation=&quot;REQUIRED&quot;/&gt;</span><br><span class="line">    		&lt;tx:method name=&quot;delete*&quot; propagation=&quot;REQUIRED&quot;/&gt;</span><br><span class="line">    		&lt;tx:method name=&quot;update*&quot; propagation=&quot;REQUIRED&quot;/&gt;</span><br><span class="line">    		&lt;tx:method name=&quot;find*&quot; propagation=&quot;SUPPORTS&quot; read-only=&quot;true&quot;/&gt;</span><br><span class="line">    		&lt;tx:method name=&quot;get*&quot; propagation=&quot;SUPPORTS&quot; read-only=&quot;true&quot;/&gt;</span><br><span class="line">    	&lt;/tx:attributes&gt;</span><br><span class="line">    &lt;/tx:advice&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;!-- 切面 --&gt;</span><br><span class="line">    &lt;aop:config&gt;</span><br><span class="line">    	&lt;aop:advisor advice-ref=&quot;txAdvice&quot; pointcut=&quot;execution(* com.mazaiting.service.*.*(..))&quot;/&gt;</span><br><span class="line">    &lt;/aop:config&gt;</span><br><span class="line">    </span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<ul>
<li><strong>propagation=”REQUIRED”</strong>：如果当前方法执行的时候，没有事务要开启事务，如果有事务，就在当前事务中执行。也可理解为当前方法被调用的时候没有事务传递进来，就自己开启事务。</li>
<li><strong>propagation=”SUPPORTS”</strong>：如果当前方法执行的时候，如果有事务，就在当前事务中执行，如果没有事务就不开启事务。也可理解为当前方法被调用的时候没有事务传递进来，自己不开启事务。</li>
</ul>
<p>####6. 整合表现层</p>
<p>######1). 在Config源码包下创建/spring/springmvc.xml文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; </span><br><span class="line">    xmlns:p=&quot;http://www.springframework.org/schema/p&quot;</span><br><span class="line">    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;</span><br><span class="line">    xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;</span><br><span class="line">    xsi:schemaLocation=&quot;</span><br><span class="line">        http://www.springframework.org/schema/beans </span><br><span class="line">        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd</span><br><span class="line">        http://www.springframework.org/schema/mvc </span><br><span class="line">        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd</span><br><span class="line">        http://www.springframework.org/schema/context </span><br><span class="line">        http://www.springframework.org/schema/context/spring-context-4.0.xsd&quot;&gt;</span><br><span class="line"></span><br><span class="line">	&lt;!-- 配置扫描组件包 --&gt;</span><br><span class="line">	&lt;context:component-scan base-package=&quot;com.mazaiting.controller&quot;/&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;!-- 配置注解驱动，如果配置此标签，则不用配置处理器映射器和处理器适配器 --&gt;</span><br><span class="line">	&lt;mvc:annotation-driven /&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;!-- 配置视图解析器 --&gt;</span><br><span class="line">	&lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;</span><br><span class="line">		&lt;!-- 前缀 --&gt;</span><br><span class="line">		&lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/jsp/&quot;/&gt;</span><br><span class="line">		&lt;!-- 后缀 --&gt;</span><br><span class="line">		&lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot;/&gt;</span><br><span class="line">	&lt;/bean&gt;</span><br><span class="line">	</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<p>######2). 配置前端控制器，在/WEB-INF/目录下创建web.xml文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">	xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;</span><br><span class="line">	xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span><br><span class="line">	id=&quot;WebApp_ID&quot; version=&quot;3.0&quot;&gt;</span><br><span class="line">	&lt;display-name&gt;Spring+SpringMVC+Mybatis&lt;/display-name&gt;</span><br><span class="line">	&lt;!-- 配置欢迎页 --&gt;</span><br><span class="line">	&lt;welcome-file-list&gt;</span><br><span class="line">		&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;</span><br><span class="line">		&lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;</span><br><span class="line">		&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;</span><br><span class="line">		&lt;welcome-file&gt;default.html&lt;/welcome-file&gt;</span><br><span class="line">		&lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;</span><br><span class="line">		&lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;</span><br><span class="line">	&lt;/welcome-file-list&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;filter&gt;</span><br><span class="line">		&lt;filter-name&gt;charsetEncoding&lt;/filter-name&gt;</span><br><span class="line">		&lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;</span><br><span class="line">		&lt;init-param&gt;</span><br><span class="line">			&lt;param-name&gt;encoding&lt;/param-name&gt;</span><br><span class="line">			&lt;param-value&gt;UTF-8&lt;/param-value&gt;</span><br><span class="line">		&lt;/init-param&gt;</span><br><span class="line">		&lt;init-param&gt;</span><br><span class="line">			&lt;param-name&gt;forceEncoding&lt;/param-name&gt;</span><br><span class="line">			&lt;param-value&gt;true&lt;/param-value&gt;</span><br><span class="line">		&lt;/init-param&gt;</span><br><span class="line">	&lt;/filter&gt;</span><br><span class="line">	&lt;filter-mapping&gt;</span><br><span class="line">		&lt;filter-name&gt;charsetEncoding&lt;/filter-name&gt;</span><br><span class="line">		&lt;url-pattern&gt;/*&lt;/url-pattern&gt;</span><br><span class="line">	&lt;/filter-mapping&gt;</span><br><span class="line">	&lt;!-- 初始化Spring容器 --&gt;</span><br><span class="line">	&lt;context-param&gt;</span><br><span class="line">		&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;</span><br><span class="line">		&lt;param-value&gt;classpath:spring/applicationContext-*.xml&lt;/param-value&gt;</span><br><span class="line">	&lt;/context-param&gt;</span><br><span class="line">	&lt;listener&gt;</span><br><span class="line">		&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;</span><br><span class="line">	&lt;/listener&gt;</span><br><span class="line">	&lt;!-- 配置前端控制器 --&gt;</span><br><span class="line">	&lt;servlet&gt;</span><br><span class="line">		&lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;</span><br><span class="line">		&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;</span><br><span class="line">		&lt;init-param&gt;</span><br><span class="line">			&lt;!-- 指定springmvc配置文件的路径。如果不指定，默认为：/WEB-INF/$&#123;servlet-name&#125;-servlet.xml --&gt;</span><br><span class="line">			&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;</span><br><span class="line">			&lt;param-value&gt;classpath:spring/springmvc.xml&lt;/param-value&gt;</span><br><span class="line">		&lt;/init-param&gt;</span><br><span class="line">		&lt;!-- 设置首页启动 --&gt;</span><br><span class="line">		&lt;!-- &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; --&gt;</span><br><span class="line">	&lt;/servlet&gt;</span><br><span class="line">	&lt;servlet-mapping&gt;</span><br><span class="line">		&lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;</span><br><span class="line">		&lt;url-pattern&gt;*.action&lt;/url-pattern&gt;</span><br><span class="line">	&lt;/servlet-mapping&gt;</span><br><span class="line">	</span><br><span class="line">&lt;/web-app&gt;</span><br></pre></td></tr></table></figure></p>
<p>######3).  至此，基本框架即已搭好<br><img src="https://upload-images.jianshu.io/upload_images/3110861-4d95783e53871ca4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"><br><a href="https://gitee.com/mazaiting/Ssm" target="_blank" rel="noopener">代码下载–a6635b76c</a></p>
<p>####6. 使用</p>
<p>######1). 在/WebContent/jsp/目录下创建itemList.jsp文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">	pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;查询商品列表&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;form action=&quot;$&#123;pageContext.request.contextPath &#125;/item/queryitem.action&quot; method=&quot;post&quot;&gt;</span><br><span class="line">			查询条件：</span><br><span class="line">			&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">				&lt;tr&gt;</span><br><span class="line">					&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;查询&quot;/&gt;&lt;/td&gt;</span><br><span class="line">				&lt;/tr&gt;</span><br><span class="line">			&lt;/table&gt;</span><br><span class="line">			商品列表：</span><br><span class="line">			&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">				&lt;tr&gt;</span><br><span class="line">					&lt;td&gt;商品名称&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品价格&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;生产日期&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品描述&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;操作&lt;/td&gt;</span><br><span class="line">				&lt;/tr&gt;</span><br><span class="line">				&lt;c:forEach items=&quot;$&#123;itemList &#125;&quot; var=&quot;item&quot;&gt;</span><br><span class="line">					&lt;tr&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.name &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.price &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;&lt;fmt:formatDate value=&quot;$&#123;item.createtime &#125;&quot; pattern=&quot;yyyy-MM-dd HH:mm:ss&quot;/&gt;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.pic &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;&lt;a href=&quot;$&#123;pageContext.request.contextPath &#125;/itemEdit.action?id=$&#123;item.id&#125;&quot;&gt;修改&lt;/a&gt;&lt;/td&gt;</span><br><span class="line">					&lt;/tr&gt;</span><br><span class="line">				&lt;/c:forEach&gt;</span><br><span class="line">			&lt;/table&gt;</span><br><span class="line">		&lt;/form&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p>######2). 在com.mazaiting.pojo包下创建Item.java文件，此类为实体类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">public class Item &#123;</span><br><span class="line">	// 编号</span><br><span class="line">	private Integer id;</span><br><span class="line">	// 商品名称</span><br><span class="line">	private String name;</span><br><span class="line">	// 价格</span><br><span class="line">	private Float price;</span><br><span class="line">	// 图片</span><br><span class="line">	private String pic;</span><br><span class="line">	// 时间戳</span><br><span class="line">	private Timestamp createtime;</span><br><span class="line"></span><br><span class="line">	public Integer getId() &#123;</span><br><span class="line">		return id;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setId(Integer id) &#123;</span><br><span class="line">		this.id = id;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getName() &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setName(String name) &#123;</span><br><span class="line">		this.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public Float getPrice() &#123;</span><br><span class="line">		return price;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setPrice(Float price) &#123;</span><br><span class="line">		this.price = price;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getPic() &#123;</span><br><span class="line">		return pic;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setPic(String pic) &#123;</span><br><span class="line">		this.pic = pic;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public Timestamp getCreatetime() &#123;</span><br><span class="line">		return createtime;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setCreatetime(Timestamp createtime) &#123;</span><br><span class="line">		this.createtime = createtime;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Item [id=&quot; + id + &quot;, name=&quot; + name + &quot;, price=&quot; + price + &quot;, pic=&quot; + pic + &quot;, createtime=&quot; + createtime</span><br><span class="line">				+ &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######3). 创建ItemMapper接口和ItemMapper.xml文件<br><strong>ItemMapper.java</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface ItemMapper &#123;</span><br><span class="line">	List&lt;Item&gt; getItemList();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>ItemMapper.xml</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.ItemMapper&quot;&gt;</span><br><span class="line">	&lt;select id=&quot;getItemList&quot; resultType=&quot;Item&quot;&gt;</span><br><span class="line">		select * from items</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p>######4). 在com.mazaiting.service包下创建ItemService接口文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface ItemService &#123;</span><br><span class="line">	List&lt;Item&gt; getItemList();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######5). 在com.mazaiting.service.impl包下创建ItemService接口实现类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// 标记为Service</span><br><span class="line">@Service</span><br><span class="line">public class ItemServiceImpl implements ItemService &#123;</span><br><span class="line"></span><br><span class="line">	// 注入ItemMapper</span><br><span class="line">	@Autowired</span><br><span class="line">	ItemMapper mapper;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public List&lt;Item&gt; getItemList() &#123;</span><br><span class="line">		return mapper.getItemList();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######6). 创建ItemController<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">// 标记为控制器</span><br><span class="line">@Controller</span><br><span class="line">public class ItemController &#123;</span><br><span class="line">	</span><br><span class="line">	// 注入Service</span><br><span class="line">	@Autowired </span><br><span class="line">	ItemService service;</span><br><span class="line">	</span><br><span class="line">	// 设置网络访问路径/itemList.action</span><br><span class="line">	@RequestMapping(&quot;/itemList&quot;)</span><br><span class="line">	public ModelAndView itemList() &#123;</span><br><span class="line">		List&lt;Item&gt; list = service.getItemList();</span><br><span class="line">		ModelAndView modelAndView = new ModelAndView();</span><br><span class="line">		// 设置数据</span><br><span class="line">		modelAndView.addObject(&quot;itemList&quot;, list);</span><br><span class="line">		// 设置视图名称</span><br><span class="line">		modelAndView.setViewName(&quot;itemList&quot;);</span><br><span class="line">		return modelAndView;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######7). 运行项目，<code>http://localhost:8080/Ssm/itemList.action</code>即可看到效果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-1c034fe7817df400.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p>######8). 项目结构图<br><img src="https://upload-images.jianshu.io/upload_images/3110861-4c29b227d9495638.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>####7. 测试<br>1). ItemMapper测试<br>在Java Resourcecs下创建源码包test，然后创建与测试文件相同的包名，类名以要测试的类名+Test命名<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public class ItemMapperTest &#123;</span><br><span class="line">	ApplicationContext context;</span><br><span class="line">	@Before</span><br><span class="line">	public void init()&#123;</span><br><span class="line">		context = new ClassPathXmlApplicationContext(Constant.PATHS);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetItemList() &#123;</span><br><span class="line">		ItemMapper mapper = context.getBean(ItemMapper.class);</span><br><span class="line">		List&lt;Item&gt; list = mapper.getItemList();</span><br><span class="line">		for (Item item : list) &#123;</span><br><span class="line">			System.out.println(item.toString());</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>其中Constants类内容：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">public class Constant &#123;</span><br><span class="line">	// 对应的配置文件</span><br><span class="line">	public static final String[] PATHS = &#123;&quot;spring/springmvc.xml&quot;,&quot;spring/applicationContext-*.xml&quot;&#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####2). Service测试–ItemServiceTest<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public class ItemServcieTest &#123;</span><br><span class="line">	ApplicationContext context;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() &#123;</span><br><span class="line">		context = new ClassPathXmlApplicationContext(Constant.PATHS);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetItemList() &#123;</span><br><span class="line">		ItemService service = context.getBean(ItemService.class);</span><br><span class="line">		List&lt;Item&gt; list = service.getItemList();</span><br><span class="line">		for (Item item : list) &#123;</span><br><span class="line">			System.out.println(item);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><a href="https://gitee.com/mazaiting/Ssm" target="_blank" rel="noopener">代码下载–b7993e740</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Exception/JakartaEE-Struts2-The-content-of-element-type-_package_-must-match-_(r/" rel="next" title="JakartaEE-Struts2-The-content-of-element-type-_package_-must-match-_(r">
                <i class="fa fa-chevron-left"></i> JakartaEE-Struts2-The-content-of-element-type-_package_-must-match-_(r
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(三)/" rel="prev" title="JakartaEE-Spring、MyBatis和SpringMVC的整合(三)">
                JakartaEE-Spring、MyBatis和SpringMVC的整合(三) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
